package cn.wolfcode.trip.admin;

import cn.wolfcode.trip.core.domain.StrategyContent;
import cn.wolfcode.trip.core.domain.StrategyDetail;
import cn.wolfcode.trip.core.query.QueryObject;
import cn.wolfcode.trip.core.service.IStrategyDetailService;
import cn.wolfcode.trip.core.service.IStrategyService;
import cn.wolfcode.trip.core.util.JsonResult;
import cn.wolfcode.trip.core.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("strategyDetail")
public class StrategyDetailController {

    @Autowired
    private IStrategyDetailService strategyDetailService;

    @Autowired
    private IStrategyService strategyService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo")QueryObject qo, Model model){

        model.addAttribute("pageInfo",strategyDetailService.query(qo));
        model.addAttribute("strategies",strategyService.listAll());
        return "strategyDetail/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(StrategyDetail strategyDetail, MultipartFile file) {
        JsonResult json = new JsonResult();
        try {
            //上传图片
            if (file!=null&&file.getSize()>0) {
                String url = UploadUtil.uploadAli(file);
                strategyDetail.setCoverUrl(url);
            }
            strategyDetailService.saveOrUpdate(strategyDetail);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @RequestMapping("getContentById")
    @ResponseBody
    public StrategyContent getContentById(Long id){

        return strategyDetailService.getContentById(id);
    }
}
